#!/usr/bin/perl -w
###############################################################################
# $Id: Linux.pm 1075500 2011-02-28 19:30:55Z arkurth $
###############################################################################
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
###############################################################################

=head1 NAME

VCL::Module::OS::ec2.pm - EC2 module to brokered ec2 image

=head1 SYNOPSIS

 Needs to be written

=head1 DESCRIPTION

 This module provides VCL support for images brokered from ec2.

=cut

##############################################################################
package VCL::Module::OS::ec2;

# Specify the lib path using FindBin
use FindBin;
use lib "$FindBin::Bin/../../..";

# Configure inheritance
use base qw(VCL::Module::OS);

# Specify the version of this module
our $VERSION = '2.2.1';

# Specify the version of Perl to use
use 5.008000;

use strict;
use warnings;
use diagnostics;
no warnings 'redefine';

use VCL::utils;

##############################################################################

=head2 pre_capture

 Parameters  : None
 Returns     : If successful: return 1
               If failed: return 0
 Description : prepare OS before provisioning module loads the system. As in brokering, all this
		is handled by other cloud, we just return 1 to indicate necessary stuff is done
=cut
sub pre_capture {
	return 1;
}

##############################################################################

=head2 post_load

 Parameters  : None
 Returns     : If successful: return 1
               If failed: return 0
 Description : prepare OS after system is provisioned.  As in brokering, all this
                is handled by other cloud, we just return 1 to indicate necessary stuff is done
=cut
sub post_load {
        return 1;
}

##############################################################################

=head2 reserve

 Parameters  : None
 Returns     : If successful: return 1
               If failed: return 0
 Description : create user accounts and make system ready before user hits connect button. 
		For brokering, this is done on another cloud.
=cut
sub reserve {
        return 1;
}

##############################################################################

=head2 grant_access

 Parameters  : None
 Returns     : If successful: return 1
               If failed: return 0
 Description : configures the reserved machine to allow access for the users created.
		For brokering, this is done on another cloud
=cut
sub grant_access {
        return 1;
}

#/////////////////////////////////////////////////////////////////////////////

=head2 get_current_image_name

 Parameters  : None
 Returns     : If successful: string
               If failed: false
 Description : Reads the currentimage.txt file on a computer but for cloud machine we just return image name we added to DB and returns a
               string containing the name of the loaded image.

=cut

sub get_current_image_name {
	my $self = shift;
	my $reservation_id = $self->data->get_reservation_id();
	#get image id for this reservation
	my $imagename_select = "select name from image where id=(select imageid from ec2instances where reservationid=$reservation_id)";
	my @selected_rows = database_select($imagename_select);
        # Check to make sure 1 row was returned
        if (scalar @selected_rows == 0) {
                return;
        }
        elsif (scalar @selected_rows > 1) {
                return;
        }
        my $image_name = $selected_rows[0]{name};
	return $image_name;
}

#/////////////////////////////////////////////////////////////////////////////

=head2 sanitize

 Parameters  :
 Returns     : 1 on success, 0 on failure
 Description : A resource needs to be sanitized if it was successfully reserved for a reservation but not used. For cloud we make it default 1.

=cut

sub sanitize {
	return 1;
}

##############################################################################

=head2 get_public_ip_address

 Parameters  : none
 Returns     : string
 Description : Returns the public IP address assigned to the computer. Seems new.pm needs this API to be implemented.

=cut

sub get_public_ip_address {
        my $self = shift;
        if (ref($self) !~ /VCL::Module/i) {
                notify($ERRORS{'CRITICAL'}, 0, "subroutine was called as a function, it must be called as a class method");
                return;
        }
	my $reservation_id = $self->data->get_reservation_id();
	#check if this reservationid is in ec2instances table=>implies request is from load() of provisioning.
	my $select_statement = "select remoteIP AS remote_ipaddress from reservation where id=(select reservationid from ec2instances where reservationid=$reservation_id) LIMIT 1";
        my @selected_rows = database_select($select_statement);
        if (scalar @selected_rows == 0) {
               return;
        }
        for (@selected_rows) {
               my %image_row = %{$_};
               return $image_row{remote_ipaddress};
        }
	return;
}
